jquery教程

推荐列表 站点导航

当前位置:首页 > jquery > jquery教程 >

JS验证密码强度的实例代码

来源:网络整理  作者:  发布时间:2020-12-21 18:41
本文分享下,js验证密码强度的一段代码,有需要的朋友,可以作个参考。...
1,js代码 验证密码强度

如何用js代码对密码强度评分,根据得分显示密码强度呢?本文为大家提供一个验证密码强度的例子。
function checkpass(pass){ var username = document.getElementById('username').value; var score = testpass(pass.value,username); var password_label = document.getElementById('password_label'); if(score == -4) { password_label.innerHTML = '太短'; }else if(score == -2){ password_label.innerHTML = '与用户名相同'; }else{ var color = score < 34 ? '#edabab' : (score < 68 ? '#ede3ab' : '#d3edab'); var text = score < 34 ? '弱' : (score < 68 ? '一般' : '很好'); var width = score + '%'; password_label.innerHTML = "<span>"+text+"</span>"; } } </script> 请输入用户名:<br> <input type="text" class="inpt" name="username" style="width:160px" id="username" /><br> 请输入密码:<br> <input type="password" class="inpt" style="width:160px" onkeyup="javascript:checkpass(this)" name="pass" id="pass" /><br> <span id="password_label" style="width:160px;border:1px solid #F0F0F0"></span>

function testpass(password,username){ var score = 0; if (password.length < 4 ) { return -4; } if (typeof(username) != 'undefined' && password.toLowerCase() == username.toLowerCase()){return -2} score += password.length * 4; score += ( repeat(1,password).length - password.length ) * 1; score += ( repeat(2,password).length - password.length ) * 1; score += ( repeat(3,password).length - password.length ) * 1; score += ( repeat(4,password).length - password.length ) * 1; if (password.match(/(.*[0-9].*[0-9].*[0-9])/)){ score += 5;} if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){ score += 5 ;} if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){ score += 10;} if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)){ score += 15;} if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)){ score += 15;} if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)){score += 15;} if (password.match(/^\w+$/) || password.match(/^\d+$/) ){ score -= 10;} if ( score < 0 ){score = 0;} if ( score > 100 ){ score = 100;} return score; function repeat(len,str){ var res = ""; for (var i = 0; i < str.length; i++ ){ var repeated = true; for (var j = 0, max = str.length - i - len; j < len && j < max; j++){ repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + len)); } if (j < len) repeated = false; if (repeated) { i += len - 1; repeated = false; }else{ res += str.charAt(i); } } return res; } }

2,例子,使用以上的函数可对密码进行评分验证。

相关热词: 实例

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jq/jc/6743.shtml

相关文章
最新文章
PHP识别相片是否是颠倒的 PHP识别相片是否是颠倒的

时间:2020-12-28

python编程有哪些ide python编程有哪些ide

时间:2020-12-28

python开发工程师是做什么 python开发工程师是做什么

时间:2020-12-28

php构造函数的作用 php构造函数的作用

时间:2020-12-28

php怎么跟数据库连接 php怎么跟数据库连接

时间:2020-12-28

php实现顺序线性表 php实现顺序线性表

时间:2020-12-28

Python多重继承中的菱形继 Python多重继承中的菱形继

时间:2020-12-28

php中break的作用 php中break的作用

时间:2020-12-28

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

JS验证密码强度的实例代码

2020-12-21 编辑:

1,js代码 验证密码强度

如何用js代码对密码强度评分,根据得分显示密码强度呢?本文为大家提供一个验证密码强度的例子。
function checkpass(pass){ var username = document.getElementById('username').value; var score = testpass(pass.value,username); var password_label = document.getElementById('password_label'); if(score == -4) { password_label.innerHTML = '太短'; }else if(score == -2){ password_label.innerHTML = '与用户名相同'; }else{ var color = score < 34 ? '#edabab' : (score < 68 ? '#ede3ab' : '#d3edab'); var text = score < 34 ? '弱' : (score < 68 ? '一般' : '很好'); var width = score + '%'; password_label.innerHTML = "<span>"+text+"</span>"; } } </script> 请输入用户名:<br> <input type="text" class="inpt" name="username" style="width:160px" id="username" /><br> 请输入密码:<br> <input type="password" class="inpt" style="width:160px" onkeyup="javascript:checkpass(this)" name="pass" id="pass" /><br> <span id="password_label" style="width:160px;border:1px solid #F0F0F0"></span>

function testpass(password,username){ var score = 0; if (password.length < 4 ) { return -4; } if (typeof(username) != 'undefined' && password.toLowerCase() == username.toLowerCase()){return -2} score += password.length * 4; score += ( repeat(1,password).length - password.length ) * 1; score += ( repeat(2,password).length - password.length ) * 1; score += ( repeat(3,password).length - password.length ) * 1; score += ( repeat(4,password).length - password.length ) * 1; if (password.match(/(.*[0-9].*[0-9].*[0-9])/)){ score += 5;} if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){ score += 5 ;} if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){ score += 10;} if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)){ score += 15;} if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)){ score += 15;} if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)){score += 15;} if (password.match(/^\w+$/) || password.match(/^\d+$/) ){ score -= 10;} if ( score < 0 ){score = 0;} if ( score > 100 ){ score = 100;} return score; function repeat(len,str){ var res = ""; for (var i = 0; i < str.length; i++ ){ var repeated = true; for (var j = 0, max = str.length - i - len; j < len && j < max; j++){ repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + len)); } if (j < len) repeated = false; if (repeated) { i += len - 1; repeated = false; }else{ res += str.charAt(i); } } return res; } }

2,例子,使用以上的函数可对密码进行评分验证。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jq/jc/6743.shtml

相关文章

风云图片

推荐阅读

返回jquery教程频道首页